<?php
/**
 *
 * Dashboard
 *
 */
use Fox\Behaviors;

use Fox\Html\Widget;
use Fox\Generator;

$xview = Xview::instance();
$layout = $xview->val('layout',null);
echo $xview->messages();

$user = User::current();
$widgetPreferences = $user->widgets();
// Columns Cusomizing
$dashboardLayout = $user->dashboardLayout($layout);
$nbOfColumns = !empty($dashboardLayout) ? $dashboardLayout : 3;
for($i=1;$i<=$nbOfColumns;$i++) {
	${'col_'.$i} = null;
}

if(empty($widgetPreferences)) {
	// Default widgets will be display
	$widgets = Fox\Dao::finder(Hive\Widget::className())
	->where('isdefault = ?',array(1))
	->find();
	// Save It to use preference
	$columns = array(1=>1,2=>1,3=>1);
	$i = 0;
	foreach($widgets as $widgetObj) {
		$i++;
		$params = $widgetObj->params();
		$widget = new Widget($widgetObj->getId(),$widgetObj->label(),$widgetObj->uri(),$params);
		${'col_'.$i} .= $widget->display();
		$columns[$i]++;
		$preferences[$widgetObj->getId()] = array('widgetid' => $widgetObj->getId(),'column' => $i,'position' => $columns[$i]);
		if($i == $nbOfColumns) {
			$i=0;
		}
	}
	if(!empty($preferences)) {
		$user->setPrefs(User::PREFERENCE_WIDGETS,json_encode($preferences));
	}
} else {
	foreach($widgetPreferences as $column => $content) {
		ksort($content);
		$i = 0;
		foreach($content as $data) {
			try {
				$widgetObj = Fox\Dao::load(Hive\Widget::className(),$data['widgetid']);
				$params = $widgetObj->params();
				$params = array_merge($params,$data);
				$widget = new Widget($widgetObj->getId(),$widgetObj->label(),$widgetObj->uri(),$params);
				if($column > $nbOfColumns) {
					// We are going dispatch them at the end of each remaining columns
					$i++;
					$column = $i;
					
					if($i == $nbOfColumns) {
						$i=0;
					}
				}
				${'col_'.$column} .= $widget->display();
			} catch(Fox\Object\Exception\NotExist $e) {
				continue;
			}
		}
	}
}


$behaviorCustomizeDahsboard = new Behaviors(Behaviors::action(Generator::url(
	'dashboard','customize'
),true));
?>
<div class="widgets-toolbar">
	<span class="widget-header"><?php echo Msg(Hive::BUNDLE,Hive::DASHBOARD);?></span>
	<div class="btn-toolbar">
	<div class="actions btn-group">
	 <a href="?mod=dashboard&layout=1" title="<?php echo Msg(Hive::BUNDLE,Hive::DASHBOARD_1_COL); ?>" class="btn <?php if($nbOfColumns == 1) {echo 'active';} ?> hastooltip" ><i class="icon-th-list" data-position="bottom" ></i></a>
	 <a href="?mod=dashboard&layout=2" title="<?php echo Msg(Hive::BUNDLE,Hive::DASHBOARD_2_COL); ?>" class="btn <?php if($nbOfColumns == 2) {echo 'active';} ?> hastooltip" style="margin-left: -2px;" data-position="bottom" ><i class="icon-th-large"></i></a>
	 <a href="?mod=dashboard&layout=3" title="<?php echo Msg(Hive::BUNDLE,Hive::DASHBOARD_3_COL); ?>" class="btn <?php if($nbOfColumns == 3) {echo 'active';} ?> hastooltip" data-position="bottom" ><i class="icon-th"></i></a> 
	 <a title="<?php echo Msg(Hive::BUNDLE,Hive::DASHBOARD_CUSTOMIZE); ?>" class="btn hastooltip <?php echo $behaviorCustomizeDahsboard->serialize(array(),'dashboard');?>" href="#" style="margin-left: -2px;" data-position="bottom" ><i class="icon-wrench"></i></a>
	</div>
	</div>
</div>
<div class="widgets-container">
	<div class="widgets-container-inner">
		<div style="width: 100%; height: 100%; padding: 0px; margin: 0px;" aria-hidden="false">
			<div style="position: relative; overflow: hidden; width: 100%; height: 100%;" aria-hidden="false">
				<div>
					<div style="width: 100%; height: 100%; padding: 0px; margin: 0px;" aria-hidden="false">
						<div style="width: 100%; height: 100%;" aria-hidden="false">
							<?php 
								for($i=1;$i<=$nbOfColumns;$i++) {
									$width = round(100/$nbOfColumns);
									echo '<div id="column'.$i.'" style="width:'.$width.'%" data-column="'.$i.'" class="columns column'.$i.' dragdrop-dropTarget">'.${'col_'.$i}.'</div>';
								}
							?>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
